#include "vector.h"

Vector2D::Vector2D(void)
{
	x = 0.0f;
	y = 0.0f;
}


Vector2D::~Vector2D(void)
{

}

Vector2D::Vector2D(GLfloat xx, GLfloat yy)
{
	x = xx;
	y = yy;
}

Vector2D::Vector2D(const Vector2D &vector)
{
	x = vector.x;
	y = vector.y;
}

Vector2D Vector2D::operator+(const Vector2D &vect)
{
	return Vector2D(x+vect.x, y+vect.y);
}

Vector2D Vector2D::operator-(const Vector2D &vect)
{
	return Vector2D(x-vect.x, y-vect.y);
}

const Vector2D Vector2D::operator*(const GLfloat val) const
{
	return Vector2D(x*val, y*val);
}

const Vector2D Vector2D::operator/(const GLfloat val) const
{
	return Vector2D(x/val, y/val);
}


void Vector2D::operator*=(const GLfloat val)
{
	x *= val;
	y *= val;
}

void Vector2D::operator+=(const Vector2D &vect)
{
	x += vect.x;
	y += vect.y;
}

void Vector2D::operator-=(const Vector2D &vect)
{
	x -= vect.x;
	y -= vect.y;
}


Vector2D Vector2D::operator-()
{
	return Vector2D(-x, -y);
}

const Vector2D &Vector2D::operator=(const Vector2D &vect)
{
	x = vect.x;
	y = vect.y;
	
	return vect;
}

void Vector2D::addScaledVector(Vector2D &v, float scale)
{
	x += v.x*scale;
	y += v.y*scale;
}

void Vector2D::addScaledVector(Vector2D *v, float scale)
{
	x += v->x*scale;
	y += v->y*scale;
}

void Vector2D::add(Vector2D *vect)
{
	x += vect->x;
	y += vect->y;
}

GLfloat Vector2D::getX(void)
{
	return x;
}

GLfloat Vector2D::getY(void)
{
	return y;
}

void Vector2D::setX(GLfloat xx)
{
	x = xx;
}

void Vector2D::setY(GLfloat yy)
{
	y = yy;
}

void Vector2D::reduce(GLfloat factor)
{
	x *= factor;
	y *= factor;
}

bool Vector2D::isNull(void)
{
	return (x == 0.0f && y == 0.0f);
}

void Vector2D::mul(GLfloat val)
{
	x *= val;
	y *= val;
}

void Vector2D::clear(void)
{
	x = 0.0f;
	y = 0.0f;
}

float Vector2D::squaredMagnitude(void)
{
	return x*x + y*y;
}

/* TODO: ottimizzabile */
float Vector2D::magnitude(void)
{
	return sqrt(x*x + y*y);
}

void Vector2D::normalize(void)
{
	float m = magnitude();
	
	if (m > 0)
		(*this)*=1.0f/m;
	
}
